package io.renren.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @Description 字符处理工具类
 * @Author haijun.zhang@luckincoffee.com
 * @Date 2018-08-08 16:13
 **/
public class CharUtils {

    public static String toEncodeExistCommon(String s) {
        char[] ch = s.toCharArray();
        String result = "";
        for (int i = 0; i < ch.length; i++) {
            char temp = ch[i];
            if (isCommon(temp)) {
                try {
                    String encode = URLEncoder.encode(String.valueOf(temp), "utf-8");
                    result = result + encode;
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                result = result + temp;
            }
        }
        return result;
    }

    /**
     * 判断是非为键盘类型 英文，数组 符号。
     * @param c
     * @return
     */
    private static boolean isCommon(char c) {

        Character.UnicodeScript sc = Character.UnicodeScript.of(c);
        if (sc != Character.UnicodeScript.COMMON) {
            return true;
        }
        return false;
    }
}
